/* xx5.7cim.p - Gen CIM for Importing PO (from eb2.1 to 2015EE)
   Created By: Hayden (2015EE) 2016-06-14
   
   Purpose: Gen CIM for importing PO
   Runtime: 2015EE
   Notice : After doing cimload, if *.prn file has any errors,
            you need generate and execute new cim file again.
   
   Upgrade Step:
     1. run xx5.7exp.p in eb2.1 generate *.txt files
     2. ftp *.txt files to 2015EE
     3. run this program gen *.cim for each domain in 2015EE
     4. doing cimload with *.cim for each domain in 2015EE
*/

/* import global_domain, base_curr */
{us/mf/mfdtitle.i}

def temp-table tpo_mstr
  field tpo_nbr       like po_nbr
  field tpo_vend      like po_vend
  field tpo_ship      like po_ship
  field tpo_ord_date  like po_ord_date 
  field tpo_due_date  like po_due_date
  field tpo_buyer     like po_buyer
  field tpo_bill      like po_bill
  field tpo_contract  like po_contract
  field tpo_contact   like po_contact
  field tpo_rmks      like po_rmks
  field tpo_site      like po_site
  field tpo_project   like po_project
  field tpo_confirm   like po_confirm
  field tpo_curr      like po_curr
  field tpo_lang      like po_lang
  field tpo_taxable   like po_taxable
  field tpo_taxc      like po_taxc
  field tpo_tax_date  like po_tax_date
  field tpo_fix_pr    like po_fix_pr
  field tpo_cr_terms  like po_cr_terms
  field tpo_cmtindx   like po_cmtindx
  field tpo_tax_usage like po_tax_usage
  field tpo_tax_env   like po_tax_env
  index itpo_nbr is primary tpo_nbr
  index itpo_cmtindx tpo_cmtindx.

def temp-table tcmt_det
  field tcmt_indx like cmt_indx
  field tcmt_seq  like cmt_seq
  field tcmt_ref  like cmt_ref
  field tcmt_lang like cmt_lang
  field tcmt_type like cmt_type
  field tcmt_cmmt like cmt_cmmt
  index itcmt_indx is primary tcmt_indx.

def temp-table tpod_det
  field tpod_nbr       like pod_nbr
  field tpod_line      like pod_line
  field tpod_site      like pod_site
  field tpod_req_nbr   like pod_req_nbr
  field tpod_part      like pod_part
  field tpod_qty_ord   like pod_qty_ord
  field tpod_um        like pod_um
  field tpod_pur_cost  like pod_pur_cost
  field tpod_disc_pct  like pod_disc_pct
  field tpod_lot_rcpt  like pod_lot_rcpt
  field tpod_loc       like pod_loc
  field tpod_rev       like pod_rev
  field tpod_status    like pod_status
  field tpod_vpart     like pod_vpart
  field tpod_due_date  like pod_due_date
  field tpod_per_date  like pod_per_date
  field tpod_need      like pod_need
  field tpod_so_job    like pod_so_job
  field tpod_fix_pr    like pod_fix_pr
  field tpod_acct      like pod_acct
  field tpod_sub       like pod_sub
  field tpod_cc        like pod_cc
  field tpod_project   like pod_project
  field tpod_type      like pod_type
  field tpod_taxable   like pod_taxable
  field tpod_taxc      like pod_taxc
  field tpod_insp_rqd  like pod_insp_rqd
  field tpod_um_conv   like pod_um_conv
  field tpod_cst_up    like pod_cst_up
  field tpod_tax_usage like pod_tax_usage
  field tpod_tax_env   like pod_tax_env
  index itpod_nbr_line is primary tpod_nbr tpod_line.

/* empty temp-table tpo_mstr */
function emptypo returns logical ():
  for each tpo_mstr exclusive-lock:
    delete tpo_mstr.
  end.
  return yes.
end.

/* empty temp-table tcmt_det*/
function emptycmt returns logical ():
  for each tcmt_det exclusive-lock:
    delete tcmt_det.
  end.
  return yes.
end.

/* empty temp-table tpod_det */
function emptypod returns logical ():
  for each tpod_det exclusive-lock:
    delete tpod_det.
  end.
  return yes.
end.
  
function readpo returns logical (input file as char):
  emptypo().
  input from value(file).
  def var nbr       as char.
  def var vend      as char.
  def var ship      as char.
  def var ord_date  as char.
  def var due_date  as char.
  def var buyer     as char.
  def var bill      as char.
  def var contract  as char.
  def var contact   as char.
  def var rmks      as char.
  def var site      as char.
  def var project   as char.
  def var confirm   as char.
  def var curr      as char.
  def var lang      as char.
  def var taxable   as char.
  def var taxc      as char.
  def var tax_date  as char.
  def var fix_pr    as char.
  def var cr_terms  as char.
  def var cmtindx   as char.
  def var tax_usage as char.
  def var tax_env   as char.
  def var i         as int initial 0.
  repeat:
    import delimiter "|" nbr vend ship ord_date due_date
                         buyer bill contract contact rmks
                         site project confirm curr lang
                         taxable taxc tax_date fix_pr
                         cr_terms cmtindx tax_usage tax_env.
    if i > 0 then do:
      create tpo_mstr.
      assign tpo_nbr       = nbr
             tpo_vend      = vend
             tpo_ship      = ship
             tpo_ord_date  = date(ord_date)
             tpo_due_date  = date(due_date)
             tpo_buyer     = buyer
             tpo_bill      = bill
             tpo_contract  = contract
             tpo_contact   = contact
             tpo_rmks      = rmks
             tpo_site      = site
             tpo_project   = project
             tpo_confirm   = logical(confirm)
             tpo_curr      = curr
             tpo_lang      = lang
             tpo_taxable   = logical(taxable)
             tpo_taxc      = taxc
             tpo_tax_date  = date(tax_date)
             tpo_fix_pr    = logical(fix_pr)
             tpo_cr_terms  = cr_terms
             tpo_cmtindx   = int(cmtindx)
             tpo_tax_usage = tax_usage
             tpo_tax_env   = tax_env.
      release tpo_mstr.
    end.
    i = i + 1.
  end.
  return yes.
end.

function readcmt returns logical (input file as char):
  emptycmt().
  input from value(file).
  def var indx as char.
  def var seq as char.
  def var ref as char.
  def var lang as char.
  def var type as char.
  def var cmmt as char extent 15.
  def var i as int initial 0.
  repeat:
    import delimiter "|" indx seq ref lang type cmmt[1] cmmt[2]
                         cmmt[3] cmmt[4] cmmt[5] cmmt[6] cmmt[7]
                         cmmt[8] cmmt[9] cmmt[10] cmmt[11]
                         cmmt[12] cmmt[13] cmmt[14] cmmt[15].
    if i > 0 then do:
      create tcmt_det.
      assign tcmt_indx     = int(indx)
             tcmt_seq      = int(seq)
             tcmt_ref      = ref
             tcmt_lang     = lang
             tcmt_type     = type
             tcmt_cmmt[1]  = cmmt[1]
             tcmt_cmmt[2]  = cmmt[2]
             tcmt_cmmt[3]  = cmmt[3]
             tcmt_cmmt[4]  = cmmt[4]
             tcmt_cmmt[5]  = cmmt[5]
             tcmt_cmmt[6]  = cmmt[6]
             tcmt_cmmt[7]  = cmmt[7]
             tcmt_cmmt[8]  = cmmt[8]
             tcmt_cmmt[9]  = cmmt[9]
             tcmt_cmmt[10] = cmmt[10]
             tcmt_cmmt[11] = cmmt[11]
             tcmt_cmmt[12] = cmmt[12]
             tcmt_cmmt[13] = cmmt[13]
             tcmt_cmmt[14] = cmmt[14]
             tcmt_cmmt[15] = cmmt[15].
      release tcmt_det.
    end.
    i = i + 1.
  end.
  return yes.
end.

function readpod returns logical (input file as char):
  emptypod().
  input from value(file).
  def var nbr       as char.
  def var line      as char.
  def var site      as char.
  def var req_nbr   as char.
  def var part      as char.
  def var qty_ord   as char.
  def var um        as char.
  def var pur_cost  as char.
  def var disc_pct  as char.
  def var lot_rcpt  as char.
  def var loc       as char.
  def var rev       as char.
  def var stat      as char.
  def var vpart     as char.
  def var due_date  as char.
  def var per_date  as char.
  def var need      as char.
  def var so_job    as char.
  def var fix_pr    as char.
  def var acct      as char.
  def var sub       as char.
  def var cc        as char.
  def var project   as char.
  def var type      as char.
  def var taxable   as char.
  def var taxc      as char.
  def var insp_rqd  as char.
  def var um_conv   as char.
  def var cst_up    as char.
  def var tax_usage as char.
  def var tax_env   as char.
  def var i         as int initial 0.
  repeat:
    import delimiter "|" nbr line site req_nbr part qty_ord
                         um pur_cost disc_pct lot_rcpt loc
                         rev stat vpart due_date per_date
                         need so_job fix_pr acct sub cc
                         project type taxable taxc
                         insp_rqd um_conv cst_up tax_usage tax_env.
    if i > 0 then do:
      create tpod_det.
      assign tpod_nbr       = nbr
             tpod_line      = int(line)
             tpod_site      = site
             tpod_req_nbr   = req_nbr
             tpod_part      = part
             tpod_qty_ord   = decimal(qty_ord)
             tpod_um        = um
             tpod_pur_cost  = decimal(pur_cost)
             tpod_disc_pct  = decimal(disc_pct)
             tpod_lot_rcpt  = logical(lot_rcpt)
             tpod_loc       = loc
             tpod_rev       = rev
             tpod_status    = stat
             tpod_vpart     = vpart
             tpod_due_date  = date(due_date)
             tpod_per_date  = date(per_date)
             tpod_need      = date(need)
             tpod_so_job    = so_job
             tpod_fix_pr    = logical(fix_pr)
             tpod_acct      = acct
             tpod_sub       = sub
             tpod_cc        = cc
             tpod_project   = project
             tpod_type      = type
             tpod_taxable   = logical(taxable)
             tpod_taxc      = taxc
             tpod_insp_rqd  = logical(insp_rqd)
             tpod_um_conv   = decimal(um_conv)
             tpod_cst_up    = logical(cst_up)
             tpod_tax_usage = tax_usage
             tpod_tax_env   = tax_env.
      release tpod_det.
    end.
    i = i + 1.
  end.
  return yes.
end.

function gencim returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  def var is_update_supplier_quote as logical initial no.
  for each tpo_mstr no-lock:
  
    /* gen cim for po_mstr */
    put unformatted '@@batchload popomt.p' skip.
    put unformatted '"' tpo_nbr '"' skip.
    put unformatted '"' tpo_vend '"' skip.
    put unformatted '"' tpo_ship '"' skip.
    put unformatted '"' tpo_ord_date '" "' tpo_due_date '" "' tpo_buyer '" "' tpo_bill '" - "' tpo_contract '" "' tpo_contact '" "' tpo_rmks '" - - - "' tpo_site '" - "' tpo_project '" "' tpo_confirm '" - '.
    
    /* create or update */
    def var haspo as logical init no.
    find first po_mstr where po_domain = global_domain and po_nbr = tpo_nbr no-lock no-error.
    haspo = available(po_mstr).
    
    /* warning: ignore language:
    if not haspo then put unformatted '"' tpo_curr '" "' tpo_lang '" '.
    */
    if not haspo then put unformatted '"' tpo_curr '" - '.
    
    put unformatted '"' tpo_taxable '" "' tpo_taxc '" "' tpo_tax_date '" "' tpo_fix_pr '" "' tpo_cr_terms '"'.
    if tpo_cmtindx > 0 then put ' - - yes'. /* set Comments: yes */
    put unformatted skip.
    
    if tpo_curr <> base_curr then put '-' skip.
    put unformatted '-' skip. /* warning: ignore tax usage: put unformatted '"' tpo_tax_usage '" "' tpo_tax_env '" "' tpo_taxc '" "' tpo_taxable '" -' skip. */
    put unformatted '""' skip. /* ESR Option skip */
    
    /* handle Comments */
    if tpo_cmtindx > 0 then do: /* if need to import comments */
      for each tcmt_det no-lock where tcmt_indx = tpo_cmtindx break by tcmt_seq:
        if (haspo and po_cmtindx > 0) or tcmt_seq > 0 then put unformatted (tcmt_seq + 1) skip.
        
        def var newadd as logical init no.        
        if (haspo and po_cmtindx > 0) then do:
          find first cmt_det where cmt_domain = global_domain
                               and cmt_indx = po_cmtindx
                               and cmt_seq = tcmt_seq
                               no-lock no-error.
          newadd = not available(cmt_det).
        end.
        else newadd = yes.
        
        if newadd then put unformatted '"' tcmt_ref '" "' tcmt_type '" "' tcmt_lang '" ' (tcmt_seq + 1) skip.
        
        put unformatted '"' tcmt_cmmt[1]  '" '
                        '"' tcmt_cmmt[2]  '" '
                        '"' tcmt_cmmt[3]  '" '
                        '"' tcmt_cmmt[4]  '" '
                        '"' tcmt_cmmt[5]  '" '
                        '"' tcmt_cmmt[6]  '" '
                        '"' tcmt_cmmt[7]  '" '
                        '"' tcmt_cmmt[8]  '" '
                        '"' tcmt_cmmt[9]  '" '
                        '"' tcmt_cmmt[10] '" '
                        '"' tcmt_cmmt[11] '" '
                        '"' tcmt_cmmt[12] '" '
                        '"' tcmt_cmmt[13] '" '
                        '"' tcmt_cmmt[14] '" '
                        '"' tcmt_cmmt[15] '"'
                        skip.
        put unformatted '-' skip.
      end.
      put unformatted '.' skip.
    end.
    
    /* gen cim for pod_det */
    for each tpod_det no-lock where tpod_nbr = tpo_nbr:
      /* create or update */
      def var haspod as logical init no.
      find first pod_det where pod_nbr = tpod_nbr and pod_line = tpod_line no-lock no-error.
      haspod = available(pod_det).
      
      put unformatted tpod_line skip.
      put unformatted '"' tpod_site '"' skip.
      if not haspod then do:
      
        put unformatted '-' skip. /* warning: put unformatted '"' tpod_req_nbr '"' skip. */
        
        put unformatted '"' tpod_part '"' skip.
      end.
      
      put unformatted tpod_qty_ord.
      if not haspod then put unformatted ' "' tpod_um '"'.
      put unformatted skip.
      
      put unformatted tpod_pur_cost ' ' tpod_disc_pct skip.
      
      /* warning: ignore Supplier Part
      put unformatted '"' tpod_lot_rcpt '" "' tpod_loc '" "' tpod_rev '" - "' tpod_status '" "' tpod_vpart '" "' tpod_due_date '" "' tpod_per_date '" "' tpod_need '" "' tpod_so_job '" "' tpod_fix_pr '" "' tpod_acct '" "' tpod_sub '" "' tpod_cc '" "' tpod_project '" '.
      */
      
      /* warning: ignore pod_acct, pod_sub, pod_cc
      put unformatted '"' tpod_lot_rcpt '" "' tpod_loc '" "' tpod_rev '" - "' tpod_status '" - "' tpod_due_date '" "' tpod_per_date '" "' tpod_need '" "' tpod_so_job '" "' tpod_fix_pr '" "' tpod_acct '" "' tpod_sub '" "' tpod_cc '" "' tpod_project '" '.
      */
      put unformatted '"' tpod_lot_rcpt '" "' tpod_loc '" "' tpod_rev '" - "' tpod_status '" - "' tpod_due_date '" "' tpod_per_date '" "' tpod_need '" "' tpod_so_job '" "' tpod_fix_pr '" - - - "' tpod_project '" '.
      
      if not haspod then put unformatted '"' tpod_type '" '.
      
      put unformatted '"' tpod_taxable '" "' tpod_taxc '" "' tpod_insp_rqd '" - '.
      
      if not haspod then put unformatted tpod_um_conv ' '.
      
      put unformatted '"' tpod_cst_up '"' skip.
      
      if tpod_taxable then do:
        put '-' skip. /* warning: ignore tax usage: put unformatted '"' tpod_tax_usage '" "' tpod_tax_env '" "' tpod_taxc '" "' tpod_taxable '" -' skip. */
      end.
      
      find first vp_mstr where vp_domain = global_domain
                           and vp_part = tpod_part
                           and vp_vend = tpo_vend
                           and vp_curr = tpo_curr
                           no-lock no-error.
      if available(vp_mstr) and vp_um = tpod_um
                            and vp_q_price <> tpod_pur_cost
                            then do:
        put unformatted '"' is_update_supplier_quote '"' skip.
      end.
      
      put unformatted '-' skip.
    end.
    
    put unformatted '@@end' skip.
  end.
  output close.
  return yes.
end.

/* xx5.7cim.p - Gen CIM for Importing PO (from eb2.1 to 2015EE)
   Created By: Hayden (2015EE) 2016-06-14
   
   Purpose: Gen CIM for importing PO (from eb2.1 to 2015EE)
   Runtime: 2015EE
   Notice : After doing cimload, if *.prn file has any errors,
            you need generate and execute new cim file again.
   
   Upgrade Step:
     1. run xx5.7exp.p in eb2.1 generate *.txt files
     2. ftp *.txt files to 2015EE
     3. run this program gen *.cim for each domain in 2015EE
     4. doing cimload with *.cim for each domain in 2015EE
*/

def var poname  as char format 'x(16)' init '5.7-po_mstr' label 'PO  File'.
def var pofile  as char format 'x(24)'. 

def var podname as char format 'x(16)' init '5.7-pod_det' label 'POD File'.
def var podfile as char format 'x(24)'.

def var cmtname as char format 'x(16)' init '5.7-cmt_det' label 'CMT File'.
def var cmtfile as char format 'x(24)'.

def var cimname as char format 'x(16)' init '5.7-po' label  'CIM File'.
def var cimfile as char format 'x(24)'.

pofile  = global_domain + '-' + poname  + '.txt'.
podfile = global_domain + '-' + podname + '.txt'.
cmtfile = global_domain + '-' + cmtname + '.txt'.
cimfile = global_domain + '-' + cimname + '.cim'.

repeat:
  disp 'Gen CIM for importing PO (from eb2.1 to 2015EE)' skip
       'Runtime: 2015EE' skip(1)
       'Notice : After doing cimload, if *.prn file has any errors,' skip
       '         you need generate and execute new cim file again.' skip(1)
       'Upgrade Step:' skip
       '  1. run xx5.7exp.p in eb2.1 generate *.txt files' skip
       '  2. ftp *.txt files to 2015EE' skip
       '  3. run this program gen *.cim for each domain in 2015EE' skip
       '  4. doing cimload with *.cim for each domain in 2015EE' skip(1)
       poname  colon 14 '.txt' space(2) pofile  no-label view-as text skip
       podname colon 14 '.txt' space(2) podfile no-label view-as text skip
       cmtname colon 14 '.txt' space(2) cmtfile no-label view-as text skip
       cimname colon 14 '.cim' space(2) cimfile no-label view-as text skip
  with frame a width 80 side-label.
  
  prompt-for poname podname cmtname cimname with frame a editing:
    if frame-field = "poname" then do:
      if input poname <> "" then do:
        disp global_domain + '-' + input poname + '.txt' @ pofile with frame a.
      end.
      else do:
        disp '' @ pofile with frame a.
      end.
    end.
    
    if frame-field = "podname" then do:
      if input podname <> "" then do:
        disp global_domain + '-' + input podname + '.txt' @ podfile with frame a.
      end.
      else do:
        disp '' @ podfile with frame a.
      end.
    end.
    
    if frame-field = "cmtname" then do:
      if input cmtname <> "" then do:
        disp global_domain + '-' + input cmtname + '.txt' @ cmtfile with frame a.
      end.
      else do:
        disp '' @ cmtfile with frame a.
      end.
    end.
    
    if frame-field = "cimname" then do:
      if input cimname <> "" then do:
        disp global_domain + '-' + input cimname + '.cim' @ cimfile with frame a.
      end.
      else do:
        disp '' @ cimfile with frame a.
      end.
    end.
    
    status input.
    readkey.
    apply lastkey.
  end.
  
  poname  = input poname .
  podname = input podname.
  cmtname = input cmtname.
  cimname = input cimname.
  
  if poname = "" or podname = "" or cmtname = "" or cimname = "" then do:
    message 'Error: Please enter all filenames'.
    next.
  end.
  
  pofile  = global_domain + '-' + poname  + '.txt'.
  podfile = global_domain + '-' + podname + '.txt'.
  cmtfile = global_domain + '-' + cmtname + '.txt'.
  cimfile = global_domain + '-' + cimname + '.cim'.
  
  /* load PO Header */
  if readpo(pofile) <> yes then do:
    message 'Error: file ' + pofile + ' not exists'.
    next.
  end.
  
  /* load PO Comments */
  if readcmt(cmtfile) <> yes then do:
    message 'Error: file ' + cmtfile + ' not exists'.
    next.
  end.
  
  /* load PO Detail */
  if readpod(podfile) < yes then do:
    message 'Error: file ' + podfile + ' not exists'.
    next.
  end.
  
  /* generate cim */
  gencim(cimfile).
  
  message 'Completed!'.
  
end.
